package it.newinstance.jrainbow.source;


import java.util.NoSuchElementException;

/**
 * @author Luigi R. Viggiano
 * @version $Id: TaggedStringIterator.java 138 2007-06-12 12:23:29Z luigi.viggiano $
 */
public class TaggedStringIterator extends AbstractIterator<TaggedString> {
	public TaggedStringIterator(TaggedSource source) {
		super(source);
	}        
    public TaggedString next() {
        if (! hasNext()) throw new NoSuchElementException();
        
        int start = position;
        StringBuilder buffer = new StringBuilder();
        Tag currentElement = taggedChars[position].getTag();
        TaggedString result = new TaggedString(currentElement);
        while (hasNext() && 
                taggedChars[position].getTag() == currentElement) 
            buffer.append(taggedChars[position++].getChar());
        int end = position;

        result.setString(buffer.toString());
        result.setStartPosition(start);
        result.setEndPosition(end);
        
        return result;
    }    
}
